const towxml = require('../../towxml/index')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: ``,
    buttonOne: '',
    buttonTwo: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.actionType = ''
    this.refreshConfig()
    // this.parsingText()
    // this.setData({
    //   buttonOne: JSON.parse(this.data.buttonOne),
    //   buttonTwo: JSON.parse(this.data.buttonTwo),
    // })
  },
  openPage() {
    wx.$API['home/themePageAddHits']({ type: 'page' })
    let schoolConfig = wx.getStorageSync('schoolConfig')
    let userInfo = wx.getStorageSync('userInfo')
    if (schoolConfig.param1) {
      if (schoolConfig.param1.indexOf('?') !== -1) {
        schoolConfig.param1 += `&schoolCode=${schoolConfig.schoolCode}&studentNumber=${userInfo.userId}`
      } else {
        schoolConfig.param1 += `?schoolCode=${schoolConfig.schoolCode}&studentNumber=${userInfo.userId}`
      }
    }
    console.log(schoolConfig.param1, '这是跳转智联招聘的url')
    wx.navigateToMiniProgram({
      appId: 'wx08e25a163d60852c',
      path: schoolConfig.param1,
      success: () => {},
    })
  },
  goBack() {
    wx.reLaunch({
      url: '/pages/home/index',
    })
  },
  refreshConfig() {
    wx.$API['home/themePageGet']()
      .then((res) => {
        console.log(res, '获取主题页详情')
        this.setData({
          buttonOne: JSON.parse(res.data[0].buttonOne),
          buttonTwo: JSON.parse(res.data[0].buttonTwo),
          content: res.data[0].content,
        })
        console.log(this.data)
        this.parsingText()
      })
      .catch((error) => {
        console.log(error, 'error')
        wx.showToast({
          title: error,
          icon: 'none',
          duration: 2000,
        })
      })
  },
  /**
   *
   * @see 解析富文本
   */
  parsingText() {
    try {
      let result = towxml(this.data.content, 'html', {
        events: {
          tap: (e) => {
            if (e.target.dataset.data && e.target.dataset.data.attr.src) {
              wx.previewImage({
                current: e.target.dataset.data.attr.src,
                urls: [e.target.dataset.data.attr.src],
                fail: (res) => {
                  wx.showModal({
                    title: '您的网络视乎有波动~',
                    content: '',
                  })
                },
              })
            }
          },
          longpress: (e) => {
            try {
              if (
                !e ||
                !e.currentTarget ||
                !e.currentTarget.dataset ||
                !e.currentTarget.dataset.data ||
                !e.currentTarget.dataset.data.child[0]
              ) {
                return
              }
              wx.setClipboardData({
                data: e.currentTarget.dataset.data.child[0].text,
                success(res) {
                  wx.getClipboardData({
                    success: (res) => {
                      console.log(res.data) // data
                    },
                  })
                },
                fail: (err = '') => {
                  wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                  })
                  console.log(err)
                },
              })
            } catch (err) {
              console.log(err)
            }
          },
        },
      })
      this.setData({
        content: result,
      })
      console.log(result, 'result')
    } catch (err) {
      console.error('富文本解析出错: ', err)
      this.showToast({
        title: '内容解析错误,请刷新再试',
        icon: 'none',
        duration: 2000,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
